package com.zggk.lzz.seach2;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zggk.lzz.R;
import com.zggk.lzz.adapter.LGRecycleViewAdapter;
import com.zggk.lzz.adapter.LGViewHolder;
import com.zggk.lzz.mvp.MVPBaseActivity;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class Seach2Activity extends MVPBaseActivity<Seach2Contract.View, Seach2Presenter> implements Seach2Contract.View {


    @Bind(R.id.qx_te)
    TextView qxTe;
    @Bind(R.id.land_username)
    EditText landUsername;
    @Bind(R.id.ss_te)
    RelativeLayout ssTe;
    @Bind(R.id.recycle_view)
    RecyclerView recycleView;
    @Bind(R.id.wt_te)
    LinearLayout wtTe;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    private LGRecycleViewAdapter<Seach2Bean.DataBean.RecordsBean> adapter;
    private ArrayList<Seach2Bean.DataBean.RecordsBean> ListDatas = new ArrayList<>();

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_seach2;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
        listener();
    }

    private void listener() {
        landUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //如果长度为0
                if (s.length() == 0) {
                    String str = landUsername.getText().toString().trim();
                } else {
                    String str = landUsername.getText().toString().trim();
                    mPresenter.getData(str, Seach2Activity.this);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
    }

    private void initView() {
        recycleView.setHasFixedSize(true);
        recycleView.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(manager);
        adapter = new LGRecycleViewAdapter<Seach2Bean.DataBean.RecordsBean>(ListDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_sjcx;
            }

            @Override
            public void convert(LGViewHolder holder, Seach2Bean.DataBean.RecordsBean zCDBean, int position) {
                holder.setText(R.id.name_te, zCDBean.getDoinfo());
                holder.setText(R.id.scmc_te, zCDBean.getCreateUserName());
                holder.setText(R.id.bhlx_te, zCDBean.getEtype());
                holder.setText(R.id.sj_te, zCDBean.getCreateTime());
            }
        };
        recycleView.setAdapter(adapter);
    }

    @OnClick({ R.id.qx_te})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.qx_te:
                finish();
                break;
        }
    }

    @Override
    public void getDatas(Seach2Bean.DataBean dataBean) {
        ListDatas.clear();
        ListDatas.addAll(dataBean.getRecords());
        adapter.notifyDataSetChanged();
    }
}
